Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fast-stringify

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-stringify

A blazing fast stringifier that safely handles circular objects

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

fast-stringify

A tiny, blazing fast stringifier that safely handles circular objects

Table of contents

Summary

The fastest way to stringify an object will always be the native JSON.stringify, but it does not support circular objects out of the box. If you need to stringify objects that have circular references, fast-stringify is there for you! It maintains a very similar API to the native JSON.stringify, and aims to be the most performant stringifier that handles circular references.

Usage

import stringify from 'fast-stringify';

const object = {
  foo: 'bar',
  deeply: {
    recursive: {
      object: {},
    },
  },
};

object.deeply.recursive.object = object.deeply.recursive;

console.log(stringify(object));
// {"foo":"bar","deeply":{"recursive":{"object":"[ref=.deeply.recursive]"}}}
stringify
type StandardReplacer = (key: string, value: any) => any;
type CircularReplacer = (key: string, value: any, referenceKey: string) => any;

function stringify(
  value: any,
  replacer?: StandardReplacer,
  indent?: number,
  circularReplacer: CircularReplacer,
): string;

Stringifies the object passed based on the parameters you pass. The only required value is the object. The additional parameters passed will customize how the string is compiled.

  • value => the value to stringify
  • replacer => function to customize how the non-circular value is stringified (see the documentation for JSON.stringify for more details)
  • indent => number of spaces to indent the stringified object for pretty-printing (see the documentation for JSON.stringify for more details)
  • circularReplacer => function to customize how the circular value is stringified (defaults to [ref=##] where ## is the referenceKey)
    • referenceKey is a dot-separated key list reflecting the nested key the object was originally declared at

Importing

// ESM in browsers
import stringify from 'fast-stringify';

// ESM in NodeJS
import stringify from 'fast-stringify/mjs';

// CommonJS
const stringify = require('fast-stringify');

Benchmarks

Simple objects

Small number of properties, all values are primitives

Operations / secondRelative margin of error
fast-stringify598,0720.59%
fast-json-stable-stringify339,0820.86%
json-stringify-safe333,4470.46%
json-stable-stringify255,6190.71%
json-cycle194,5530.60%
decircularize141,8211.35%
Complex objects

Large number of properties, values are a combination of primitives and complex objects

Operations / secondRelative margin of error
fast-stringify97,5590.32%
json-stringify-safe59,9480.44%
fast-json-stable-stringify57,6561.14%
json-cycle51,8920.59%
json-stable-stringify39,1801.01%
decircularize27,0470.84%
Circular objects

Objects that deeply reference themselves

Operations / secondRelative margin of error
fast-stringify87,0300.51%
json-stringify-safe56,3290.49%
json-cycle48,1160.77%
decircularize25,2400.68%
fast-json-stable-stringify (not supported)00.00%
json-stable-stringify (not supported)00.00%
Special objects

Custom constructors, React components, etc

Operations / secondRelative margin of error
fast-stringify24,2500.38%
json-stringify-safe19,5260.52%
json-cycle18,4330.74%
fast-json-stable-stringify18,2020.73%
json-stable-stringify13,0410.87%
decircularize9,1750.82%

Development

Standard practice, clone the repo and npm i to get the dependencies. The following npm scripts are available:

  • benchmark => run benchmark tests against other equality libraries
  • build => build dist files with rollup
  • clean => run clean:dist and clean:mjs scripts
  • clean:dist => run rimraf on the dist folder
  • clean:mjs => run rimraf on the mjs folder
  • copy:mjs => copy and transform the ESM file generated by dist to be consumable as an .mjs file
  • dev => start webpack playground App
  • dist => run clean, build, and copy:mjs scripts
  • lint => run ESLint on all files in src folder (also runs on dev script)
  • lint:fix => run lint script, but with auto-fixer
  • prepublishOnly => run lint, typecheck, test:coverage, and dist scripts
  • release => run release-it for standard versions (expected to be installed globally)
  • release:beta => run release-it for beta versions (expected to be installed globally)
  • start => run dev
  • test => run Jest with NODE_ENV=test on all files in __tests__ folder
  • test:coverage => run same script as test with code coverage calculation
  • test:watch => run same script as test but keep persistent watcher
  • typecheck => run TypeScript types validation

Keywords

FAQs

Package last updated on 04 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc